import axios from "axios";
import { api_urls } from "./urls";

export const material_api = {
    deleteMaterial(id) {
        return axios.post(api_urls.material.delete, { id: id })
    },
    pageMaterial(materialReq) {
        return axios.post(api_urls.material.page, materialReq)
    },
    updateMaterial(material) {
        let req = { ...material }
        for (let index in material.exts) {
            const v = material.exts[index]
            let i = parseInt(index) + 1;
            req[`ext_name${i}`] = v.ext_name;
            req[`ext_value${i}`] = v.ext_value;
        }
        return axios.post(api_urls.material.update, req)
    },
    createMaterial(material) {
        let req = { ...material, id: 0 }
        for (let index in material.exts) {
            const v = material.exts[index]
            let i = parseInt(index) + 1;
            req[`ext_name${i}`] = v.ext_name;
            req[`ext_value${i}`] = v.ext_value;
        }
        return axios.post(api_urls.material.create, req)
    },
    verifyAmount(verifyAmountData) {
        return axios.post(api_urls.material.verifyAmount, verifyAmountData)
    },
    handleExtraColumns(material) {

        let columns = [];
        // return columns;
        for (let i = 1; i <= 20; i++) {
            const name = material[`ext_name${i}`]
            const value = material[`ext_value${i}`]
            // console.log(name, value)
            if (name !== undefined && name !== null) {
                columns.push({ id: i, ext_name: name, ext_value: value })
            }
        }
        return columns
    }
}

